<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/Users' }">用户列表</el-breadcrumb-item>
      <el-breadcrumb-item>添加用户</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 内容卡片区 -->
    <el-card class="box-card">
      <!-- 表单 -->
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- ref="ruleForm"获取数据，通过在el-form-item 添加属性prop="username"来定义规则 -->
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input
            type="text"
            v-model="ruleForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- email -->
        <el-form-item label="email" prop="email">
          <el-input
            type="text"
            v-model="ruleForm.email"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- 电话 -->
        <el-form-item label="电话" prop="mobile">
          <el-input
            type="text"
            v-model="ruleForm.mobile"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
            show-password
          ></el-input>
        </el-form-item>
        <!-- 确认密码 -->
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
            :plain="true"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    // 判断邮箱格式
    var checkemail = (rule, value, callback) => {
      setTimeout(() => {
        let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
        if (value) {
          if (!reg.test(value)) {
            callback(new Error("请正确输入邮箱"));
          } else {
            callback();
          }
        } else {
          callback();
        }
      }, 500);
    };
    // 判断用户名格式
    var checkusername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      }
      setTimeout(() => {
        if (value.length < 3) {
          callback(new Error("用户名长度大于3个字符"));
        } else {
          callback();
        }
      }, 500);
    };
    // 判断号码格式
    var checkmobile = (rule, value, callback) => {
      if (value) {
        setTimeout(() => {
          if (Number.isInteger(value)) {
            callback(new Error("请输入数字值"));
          } else {
            if (value.length < 6) {
              callback(new Error("必须大于6位数字"));
            } else {
              callback();
            }
          }
        }, 500);
      } else {
        callback();
      }
    };
    // 判断密码格式
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    // 二次密码判断
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      // 添加用户
      ruleForm: {
        email: "",
        mobile: "",
        username: "",
        password: "",
        checkPass: "",
      },
      // 判断规则
      rules: {
        username: [{ validator: checkusername, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        email: [{ validator: checkemail, trigger: "blur" }],
        mobile: [{ validator: checkmobile, trigger: "blur" }],
      },
    };
  },
  methods: {
    // form表单提交
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.post("users", this.ruleForm);
          if (res.meta.status === 201) {
            return this.$message({
              showClose: true,
              message: res.meta.msg,
              type: "success",
            });
          } else {
            return this.$message({
              showClose: true,
              message: res.meta.msg,
              type: "error",
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 表单数据校验实例
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.box-card {
  margin-top: 15px;
}
</style>